/**
 * Copyright (C), 2020-2021 by colley.ma
 * File Name: RedisScript.java
 * Encoding: UTF-8
 * Date: 2021年3月26日 下午4:21:37
 * History:
 */
package com.swak.frame.script;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;

public interface RedisScript {

    /**
     * @return The SHA1 of the script, used for executing Redis evalsha command.
     */
    String getSha1();

    /**
     * @return The script contents.
     */
    String getScriptAsString();


    /**
     * Creates new {@link RedisScript} from {@link String}.
     *
     * @param script must not be {@literal null}.
     * @return new instance of {@link RedisScript}.
     * @since 2.0
     */
    static RedisScript of(String script) {
        return new DefaultRedisScript(script);
    }

    /**
     * Creates new {@link RedisScript} (with throw away result) from the given {@link Resource}.
     *
     * @param resource must not be {@literal null}.
     * @return new instance of {@link RedisScript}.
     * @throws IllegalArgumentException if the required argument is {@literal null}.
     * @since 2.2
     */
    static RedisScript of(Resource resource) {

        Assert.notNull(resource, "Resource must not be null!");

        DefaultRedisScript script = new DefaultRedisScript();
        script.setLocation(resource);
        return script;
    }

    /**
     * Creates new {@link RedisScript} from {@link Resource}.
     *
     * @param resource must not be {@literal null}.
     * @param resultType must not be {@literal null}.
     * @return new instance of {@link RedisScript}.
     * @throws IllegalArgumentException if any required argument is {@literal null}.
     * @since 2.2
     */
    static RedisScript of(Resource resource, Class<T> resultType) {
        Assert.notNull(resource, "Resource must not be null!");
        Assert.notNull(resultType, "ResultType must not be null!");
        DefaultRedisScript script = new DefaultRedisScript();
        script.setLocation(resource);

        return script;
    }
}
